<template>
  <div class="login-page">
    <div class="head-box">
      <p class="top-title">李沧城市云脑</p>
    </div>
    <div class="form-box">
      <div class="top-fl"><img width="26" src="../../assets/image/login/jiao1.png" /></div>
      <div class="top-fr"><img width="26" src="../../assets/image/login/jiao1.png" /></div>
      <div class="bom-fl"><img width="26" src="../../assets/image/login/jiao1.png" /></div>
      <div class="bom-fr"><img width="26" src="../../assets/image/login/jiao1.png" /></div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input class="username-input" v-model="loginForm.username" placeholder="请输入账号" type="text"> </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input class="password-input" v-model="loginForm.password" placeholder="请输入密码" type="password"> </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaOnOff">
          <el-input class="code-input" v-model="loginForm.code" placeholder="验证码" type="text" style="width: 63%" />
          <div class="login-code">
            <img :src="codeUrl" @click="getCode" class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px">记住密码</el-checkbox>
        <el-form-item style="width: 100%">
          <el-button class="loading-btn" :loading="loading" size="medium" type="primary" style="width: 100%" @click.native.prevent="handleLogin">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>

          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'
import { setToken, getToken, setRouters } from '@/utils/auth'
import { BASE_URL } from '@/config/index'
// import { getCodeImg } from '@/api/login'

export default {
  data() {
    return {
      codeUrl: '',
      cookiePassword: '',
      loginForm: {
        username: '',
        password: '', // admin123
        rememberMe: false,
        code: '',
        uuid: '',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      this.$get('/captchaImage').then((res) => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
        if (this.captchaOnOff) {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        }
      })
      // getCodeImg().then((res) => {
      //   this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
      //   if (this.captchaOnOff) {
      //     this.codeUrl = 'data:image/gif;base64,' + res.img
      //     this.loginForm.uuid = res.uuid
      //   }
      // })
    },
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$post('/login', this.loginForm)
            .then((res) => {
              this.loading = false
              if (res.code !== 200) {
                this.getCode()
                return this.$message.error(res.msg)
              }
              setToken(res.token)
              this.$router.push({ path: '/hsindex' })

              // this.getHttpRouters()
            })
            .catch(() => {
              this.loading = false
              if (this.captchaOnOff) {
                this.getCode()
              }
            })
        }
      })
    },
    // 获取权限路由 列表
    getHttpRouters() {
      this.$get('/getRouters').then((res) => {
        let data = res.data
        data.map((item, index) => {
          // 大屏查看权限 BASE_URL
          if (item.path == 'https://qdzwapp.qingdao.gov.cn:7163/index.html#/') {
            const list = item.children
            console.log('list', list)
            // 暂无查看权限
            if (list.length === 0) {
              this.$router.push({ path: '/purview' })
            }
            setRouters(JSON.stringify(list))
            const firstPath = list[0].path // 默认跳转到第一个路由页面
            console.log('路由地址？？', firstPath)
            this.$router.push({ path: firstPath })
          } else {
            this.$router.push({ path: '/purview' })
          }
        })
      })
    },
  },
}
</script>

<style scoped lang="less">
.login-page {
  // background-color: #333;
  background: url('../../assets/image/login/bg.png');
  // display: flex;
  // justify-content: center;
  // align-items: center;
  height: 100vh;
  width: 100%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  position: relative;
  .head-box {
    background: url('../../assets/image/login/top.png');
    width: 100%;
    height: 150px;
    background-size: 100% 100%;
    top: 0;
    .top-title {
      font-size: 45px;
      background-image: linear-gradient(180deg, rgba(255, 255, 255, 1) 0, rgba(51, 221, 255, 1) 100%);
      overflow-wrap: break-word;
      color: rgba(255, 255, 255, 1);
      letter-spacing: 5.22580623626709px;
      font-family: AlimamaShuHeiTi-Bold;
      font-weight: 700;
      text-align: center;
      white-space: nowrap;
      line-height: 65px;
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }
}
.form-box {
  width: 400px;
  padding: 50px 35px 25px 35px;
  position: relative;
  border: 1px solid rgba(255, 255, 255, 0.17);
  background-color: rgba(255, 255, 255, 0.17);
  margin: 0 auto;
  margin-top: 30px;
  .top-fl {
    position: absolute;
    left: 0;
    top: 0;
  }
  .top-fr {
    position: absolute;
    right: 0;
    top: 0;
    transform: rotateY(180deg);
  }
  .bom-fl {
    position: absolute;
    left: 0;
    bottom: 0;
    transform: rotateX(180deg);
  }
  .bom-fr {
    position: absolute;
    right: 0;
    bottom: 0;
    transform: rotateX(180deg) rotateY(180deg);
  }
}
// 输入框
.loading-btn {
}

// /deep/ .username-input .el-input__inner {
//   /*no-repeat:图片不重复；10px：控制图片左边距 center：图片上下居中*/
//   background: url(../../assets/image/login/icon1.png) no-repeat 10px center;
//   padding: 0px 50px; /*输入内容左右边距控制*/
//   font-size: 20px;
//   // color: #dadada;
// }
// /deep/ .password-input .el-input__inner {
//   /*no-repeat:图片不重复；10px：控制图片左边距 center：图片上下居中*/
//   background: url(../../assets/image/login/icon2.png) no-repeat 10px center;
//   padding: 0px 50px; /*输入内容左右边距控制*/
//   font-size: 20px;
//   // color: #dadada;
// }
// /deep/ .code-input .el-input__inner {
//   /*no-repeat:图片不重复；10px：控制图片左边距 center：图片上下居中*/
//   background: url(../../assets/image/login/icon3.png) no-repeat 10px center;
//   padding: 0px 50px; /*输入内容左右边距控制*/
//   font-size: 20px;
//   // color: #dadada;
// }
.code-input {
  float: left;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 38px;
}
/deep/.el-checkbox__label {
  color: #fff;
}
// /deep/input::-webkit-input-placeholder {
//   color: #17a1e5;
//   font-size: 15px;
// }

</style>
<style></style>
